package ks.client.lobby;

import java.awt.Dimension;
import java.awt.Rectangle;

import javax.swing.GroupLayout;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import ks.client.controllers.ProcessLobbyInputController;
import ks.client.interfaces.ILobby;

/**
 * Inner GUI element that holds the primary graphical entities on the 
 * client side.
 * <p>
 * The intent is for user manager and table managers to be "dropped in" for
 * separation of implementation. This class takes advantage of the GroupLayout
 * layout manager for ease of design.
 * 
 * @author George Heineman
 */
public class LobbyPanel extends JPanel {

	// keep eclipse happy. 
	private static final long serialVersionUID = 1L;
	
	private JTextField lobbyInput = null;
	private JPanel userManagerGUI = null;
	private JPanel  tableManagerGUI = null;
	private JScrollPane tblScrollPanel = null;
	private JScrollPane userScrollPanel = null;
	private JScrollPane lobbyScrollbarPanel = null;
	private JTextArea lobbyOutput = null;
	
	/** Lobby in which we are enclosed. */
	ILobby lobby;

	/**
	 * This is the default constructor
	 */
	public LobbyPanel(ILobby lobby) {
		super();
		
		this.lobby = lobby;
		initialize();
	}


	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(850, 710);
		
		// new GroupLayout idiom in place.
		GroupLayout layout = new GroupLayout(this);
		this.setLayout(layout);
		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);

		getTblScrollPanel();
		getUserScrollPanel();
		getLobbyScrollbarPanel();
		getLobbyInput();
		
		// deal here
		layout.setVerticalGroup(layout.createSequentialGroup()
			    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
			    	.addGroup(layout.createSequentialGroup()
			    		.addComponent(getTblScrollPanel())
			    		.addComponent(getLobbyScrollbarPanel())
			    		.addComponent(getLobbyInput()))
			    	.addComponent(getUserScrollPanel()))
			    	);
		
		layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
					.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addComponent(getTblScrollPanel())
						.addComponent(getLobbyScrollbarPanel())
						.addComponent(getLobbyInput()))
					.addComponent(getUserScrollPanel()))
					);
							
	}

	/**
	 * return the user manager GUI	
	 */
	JPanel getUserManagerGUI() {
		return userManagerGUI;
	}

	/**
	 * return the table manager GUI
	 */
	JPanel getTableManagerGUI() {
		return tableManagerGUI;
	}

	/**
	 * This method initializes tblScrollPanel	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getTblScrollPanel() {
		if (tblScrollPanel == null) {
			tblScrollPanel = new JScrollPane();
			tblScrollPanel.setBounds(new Rectangle(11, 74, 581, 333));
			tblScrollPanel.setMinimumSize(new Dimension(235, 235));
			tblScrollPanel.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
			tblScrollPanel.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		}
		return tblScrollPanel;
	}

	/**
	 * This method initializes userScrollPanel	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getUserScrollPanel() {
		if (userScrollPanel == null) {
			userScrollPanel = new JScrollPane();
			userScrollPanel.setBounds(new Rectangle(603, 73, 239, 635));
			userScrollPanel.setMinimumSize(new Dimension(239, 235));
			userScrollPanel.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
			userScrollPanel.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

		}
		return userScrollPanel;
	}

	/**
	 * This method initializes lobbyScrollbarPanel	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getLobbyScrollbarPanel() {
		if (lobbyScrollbarPanel == null) {
			lobbyScrollbarPanel = new JScrollPane();
			lobbyScrollbarPanel.setBounds(new Rectangle(11, 419, 581, 226));
			lobbyScrollbarPanel.setMaximumSize(new Dimension(1581, 226));
			lobbyScrollbarPanel.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
			lobbyScrollbarPanel.setViewportView(getLobbyOutput());
			lobbyScrollbarPanel.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

		}
		return lobbyScrollbarPanel;
	}

	/**
	 * This method initializes lobbyOutput	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	public JTextArea getLobbyOutput() {
		if (lobbyOutput == null) {
			lobbyOutput = new JTextArea();
			
			// not writable directly by user.
			lobbyOutput.setEditable(false);
		}
		return lobbyOutput;
	}
	
	/**
	 * This method initializes lobbyOutput	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	JTextField getLobbyInput() {
		if (lobbyInput == null) {
			lobbyInput = new JTextField(80);
			lobbyInput.setMaximumSize(new Dimension(1581,24));
			
			lobbyInput.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent e) {
					String s = lobbyInput.getText();
					lobbyInput.setText("");

					// hand off to the newly constructed controller to process 
					new ProcessLobbyInputController(lobby).process(s);
				}
				
			});
		}
		return lobbyInput;
	}

	/**
	 * Append text to the lobby and pre-appends "\n" 
	 * 
	 * @param s
	 */
	public void append (String s) {
		String t = lobbyOutput.getText() + "\n" + s;
		lobbyOutput.setText(t);
		scrollToEnd();		
	}
	
	/**
	 * Make sure we can scroll to the end on demand.
	 */
	void scrollToEnd() {
		lobbyOutput.setCaretPosition(lobbyOutput.getDocument().getLength());
	}

	/**
	 * Drop this one in.
	 * 
	 * @param tableManagerGUI
	 */
	public void setTableManagerGUI(JPanel tg) {
		tableManagerGUI = tg;
		tblScrollPanel.setViewportView(tg);
	}


	/**
	 * Drop this one in.
	 * 
	 * @param userManagerGUI
	 */
	public void setUserManagerGUI(JPanel ug) {
		userManagerGUI = ug;
		userScrollPanel.setViewportView(ug);
	}


}  //  @jve:decl-index=0:visual-constraint="10,10"
